package com.enation.app.javashop.consumer.shop.goods;

import com.enation.app.javashop.consumer.core.event.OrderStatusChangeEvent;
import com.enation.app.javashop.core.base.message.OrderStatusChangeMsg;
import com.enation.app.javashop.core.client.goods.GoodsClient;
import com.enation.app.javashop.core.promotion.shetuan.service.ShetuanGoodsManager;
import com.enation.app.javashop.core.promotion.shetuan.service.ShetuanOperateManager;
import com.enation.app.javashop.core.promotion.shetuan.service.impl.ShetuanGoodsManagerImpl;
import com.enation.app.javashop.core.trade.order.model.dos.OrderDO;
import com.enation.app.javashop.core.trade.order.model.enums.OrderStatusEnum;
import com.enation.app.javashop.core.trade.order.model.enums.OrderTypeEnum;
import com.enation.app.javashop.core.trade.order.model.vo.OrderSkuVO;
import com.enation.app.javashop.framework.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author fk
 * @version v1.0
 * @Description: 商品购买数量变化
 * @date 2018/6/2510:13
 * @since v7.0.0
 */
@Service
public class GoodsBuyCountConsumer implements OrderStatusChangeEvent {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private ShetuanOperateManager shetuanOperateManager;



    @Override
    public void orderChange(OrderStatusChangeMsg orderMessage) {
        OrderDO order = orderMessage.getOrderDO();
        String itemsJson = order.getItemsJson();

        // 收货后更新商品的购买数量
        if (orderMessage.getNewStatus().name().equals(OrderStatusEnum.ROG.name())) {
            List<OrderSkuVO> list = JsonUtil.jsonToList(itemsJson,OrderSkuVO.class);
            this.goodsClient.updateBuyCount(list);
        }
        // 社团团购订单更新下单记录(已付款)
        if ( orderMessage.getNewStatus().name().equals(OrderStatusEnum.PAID_OFF.name())) {
            List<OrderSkuVO> list = JsonUtil.jsonToList(itemsJson,OrderSkuVO.class);
            this.shetuanOperateManager.updateBuyRecord(list,order.getMemberId());
        }
    }
}
